package study.SCADA网关解析;


import study.utils.CheckCodeCalculationUtils;
import study.utils.ASCIITranslateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 刘伟涛
 * @Date 2024/8/14 9:29
 **/
public class Parser {

    public static void main(String[] args) {

        String dataToSend2 = "\u00021H|\\^&|||1|||||2||1|20240829111940\r\u000398\r\n" +
                "\u00022P|1||||QC|||||||||^\r\u00037D\r\n" +
                "\u00023O|1|^0^1^C1^|伯乐生化多项质控品^45981(20240801)^|||20240826153351|||||||||1||||||||||O\r\u000349\r\n" +
                "\u00024R|1|^^^ALT|31.02|U/L|31.3^0.81||-1SD|||||20240826153351\r\u0003FF\r\n" +
                "\u00025L|1|N\r\u000308\r\n" +
                "\u0004";

        handle(dataToSend2);
/*        char a = 4;
        System.out.println("\u0005");
        System.out.println('\u0004'==a);
        System.out.println(a);
        System.out.println("Example\u0004Data".contains(Character.toString('\n')));

        String s = "\u00021H|\\^&|||1|||||2||1|20240823162037\r\u000312\r\n\u0004";
        System.out.println(s);*/
    }

    private static void handle(String str){
        // 分割STX

        String[] records = str.split("\\u0002");
        Map<Integer, List<String>> recordMap = new HashMap<>();
        Integer size = 1;
        recordMap.put(1,new ArrayList<String>());
        for (String record : records) {
            List<String> recordList = recordMap.get(size);
            System.out.println("record:" + record);
            if(record.trim().length() == 0){
                continue;
            }
            // 分割CR ETX
            String substring = record.substring(0, record.indexOf("\u0003") + 1);
            String[] split = record.split("\\u000D\\u0003");
            System.out.println("校验码：" +split[1]);
            // trim方法会去除CR，LF，ETX，STX，EOT等标识符
            record = split[0].trim();
            record = substring;
            recordList.add(record);
            System.out.println("data:" + record);
            recordMap.put(size,recordList);
            if(record.charAt(1) == 'L'){
                recordMap.put(++size,new ArrayList<String>());
            }
        }

        for (Integer integer : recordMap.keySet()) {
            List<String> list = recordMap.get(integer);
            if(list.size() == 0){
                continue;
            }
            for (String record : list) {
                String string = ASCIITranslateUtils.asciiToHex(record);
                String handle = CheckCodeCalculationUtils.handle(string);
            }
        }
    }


}
